package org.stone926.airschool.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stone926.airschool.bean.info.AbstractInfo;
import org.stone926.airschool.constant.ErrorCode;
import org.stone926.airschool.service.StudentService;
import org.stone926.airschool.service.TeacherService;

@Controller
public class LoginController {
	@Autowired
	private StudentService studentService;

	@Autowired
	private TeacherService teacherService;

	private Map<String, BiFunction<String, String, AbstractInfo>> loginActionMap = new HashMap<>();

	public LoginController() {
		super();
		loginActionMap.put("student", (username, password) -> {
			int sid = studentService.studentLogin(username, password);

			return sid != -1 ? loginSuccess(sid) : loginFail();
		});

		// TODO 返回一个teacher_id
		loginActionMap.put("teacher", (username, password) -> {
			return teacherService.teacherLogin(username, password) ?
					loginSuccess(-1) : loginFail();
		});
	}

	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	@CrossOrigin(value = "*", maxAge = 3600, methods = RequestMethod.POST)
	public AbstractInfo userLogin(@RequestBody LoginAjaxJsonMessage json) {
		String identity = json.getIdentity();
		String username = json.getUsername();
		String password = json.getPassword();

		System.out.println(username);
		System.out.println(password);
		System.out.println(identity);
		return loginActionMap.get(identity).apply(username, password);
	}

	static class LoginAjaxJsonMessage {
		private String username;
		private String password;
		private String identity;

		public LoginAjaxJsonMessage() {
			super();
		}

		public LoginAjaxJsonMessage(String username, String password, String identity) {
			super();
			this.username = username;
			this.password = password;
			this.identity = identity;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getIdentity() {
			return identity;
		}

		public void setIdentity(String identity) {
			this.identity = identity;
		}

		@Override
		public String toString() {
			return "LoginAjaxMessage [username=" + username + ", password=" + password + ", identity=" + identity + "]";
		}
	}

	private AbstractInfo loginSuccess(int sid) {
		return new AbstractInfo() {

			@Override
			public boolean isSuccess() {
				return true;
			}

			@Override
			public String getMessage() {
				return "登陆成功";
			}

			@Override
			public int getErrorCode() {
				return ErrorCode.LOGIN_SUCCESS;
			}

			@SuppressWarnings("unused")
			public int getStudentId() {
				return sid;
			}

		};
	}

	private AbstractInfo loginFail() {
		
		return new AbstractInfo() {
			
			@Override
			public boolean isSuccess() {
				return false;
			}
			
			@Override
			public String getMessage() {
				return "用户名或密码错误";
			}
			
			@Override
			public int getErrorCode() {
				return ErrorCode.LOGIN_FAIL;
			}
		};
	}
}
